// 
// Entify project
//  
// Author:
//       Lauri Taimila <lauri@taimila.com>
// 
// Copyright (c) 2010 Lauri Taimila
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Linq;
using System.Collections.Generic;

namespace Taimila.Entify
{
	/// <summary>
	/// EntityContextFactory is responsible of creating EnityContext objects
	/// and making sure that only one instace per context is created.
	/// Entity contexts are identified by their name.
	/// </summary>
	public static class EntityContextFactory
	{
		/// <summary>
		/// Lock object for creating entity contexts' thread safely.
		/// </summary>
		private static readonly object contextLock = new object();
		
		/// <summary>
		/// In-memory EntityContext cache. Contains all created entity context instances.
		/// </summary>
		private static Dictionary<string, EntityContext> contextCache = new Dictionary<string, EntityContext>();
		
		/// <summary>
		/// Get entity context by name. Before entity context can be accessed by it's name,
		/// it must be accessed with the EntityContextSettings at least once.
		/// </summary>
		/// <param name="contextName">
		/// Name of the context. <see cref="System.String"/>
		/// </param>
		/// <returns>
		/// Entity context. <see cref="EntityContext"/>
		/// </returns>
		public static EntityContext GetContext(string contextName)
		{
			lock(contextLock)
			{
				if(EntityContextFactory.contextCache.ContainsKey(contextName))
				{
					return EntityContextFactory.contextCache[contextName];
				}
				else
				{			
					throw new ArgumentException("Unknown EntityContext: " + contextName + ".");
				}
			}
		}
		
		/// <summary>
		/// Get entity context with given context settings.
		/// </summary>
		/// <param name="contextSettings">
		/// Context settings for entity context. <see cref="ContextSettings"/>
		/// </param>
		/// <returns>
		/// Entity context. <see cref="EntityContext"/>
		/// </returns>
		public static EntityContext GetContext(EntityContextSettings contextSettings)
		{
			lock(contextLock)
			{
				if(EntityContextFactory.contextCache.ContainsKey(contextSettings.ContextName))
				{
					return EntityContextFactory.contextCache[contextSettings.ContextName];
				}
				else
				{
					EntityContext context = new EntityContext(contextSettings);
					EntityContextFactory.contextCache.Add(contextSettings.ContextName, context);
					return context;
				}
			}
		}
	}
}
